import { updateCode } from '@/socket/updateCode';
import { Socket } from 'socket.io';

import installNodeModules from '../installNodeModules';
import EmitTask from '../utils/emitTask';

let commandTask: any = null;

export default function (socket: Socket, data: any): void {
  if (commandTask) {
    EmitTask.SEND_MSG(socket, null, 'error-end', '已有命令任务正在执行中，请稍后再试');
  } else {
    const { params, taskName } = data;
    if (taskName === 'update-code') {
      // 更新代码任务
      commandTask = taskName;
      const { appId, repoId } = params;
      updateCode(socket, appId, repoId).then(() => {
        commandTask = null;
      });
    } else if (taskName === 'install-node-modules') {
      // 安装依赖任务
      commandTask = taskName;
      installNodeModules(socket, params).then(() => {
        commandTask = null;
      });
    }
  }
}
